/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Buscador.java
 *
 * Created on 20-ene-2012, 20:24:19
 */
package Vista;

import Modelo.Busqueda;
import Modelo.Cliente;
import Modelo.ExcelTableExporter;
import Modelo.Producto;
import Modelo.Recepcion_Judicial;
import Vista.Tablas.Modelos.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Deico
 */

public class Panel_Busqueda_Causa extends javax.swing.JPanel {



    DefaultTableModel model = new DefaultTableModel();
    DefaultTableModel tmodel_causa = new Causa_Busqueda();
    
    public static ArrayList<ArrayList> lista_seleccionados = new ArrayList();
    private VentanaPrincipal V_Principal;
    private Thread tread;
    protected String RUT;
    private Panel_Remate  P_Remate;
    
    /** Creates new form Buscador */
    
    public Panel_Busqueda_Causa() {
        initComponents();
        model = tmodel_causa;
        Tabla.setModel(model);
        
    }
    
    public void Hilo(boolean start){
          
        if(start){
            tread = new taskInitContents();
            tread.start();
            
        }
        
        else
        tread.stop();
    }
    
    public void setFrame(JFrame f){
        this.V_Principal= (VentanaPrincipal) f;
    }
    
    public void setPanel(JPanel remate){
        P_Remate = (Panel_Remate) remate;
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        parametro_busqueda = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        Tabla = new javax.swing.JTable(){

            @Override
            public boolean getScrollableTracksViewportWidth() {

                if (autoResizeMode != AUTO_RESIZE_OFF) {
                    if (getParent() instanceof JViewport) {
                        return (((JViewport)getParent()).getWidth() > getPreferredSize().width);
                    }

                }return false;

            }};
            selectAll = new javax.swing.JCheckBox();
            jLabel1 = new javax.swing.JLabel();
            Bar = new javax.swing.JProgressBar();
            label_Buscar481 = new Vista.Imagenes_Label.buscar.Label_Buscar48();
            jButton_UsarSeleccionado = new javax.swing.JButton();
            jButton_Cancelar = new javax.swing.JButton();

            setMaximumSize(new java.awt.Dimension(1000, 550));
            setMinimumSize(new java.awt.Dimension(1000, 550));

            parametro_busqueda.setFont(new java.awt.Font("Arial", 0, 14));
            parametro_busqueda.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    parametro_busquedaActionPerformed(evt);
                }
            });
            parametro_busqueda.addKeyListener(new java.awt.event.KeyAdapter() {
                public void keyPressed(java.awt.event.KeyEvent evt) {
                    parametro_busquedaKeyPressed(evt);
                }
                public void keyReleased(java.awt.event.KeyEvent evt) {
                    parametro_busquedaKeyReleased(evt);
                }
                public void keyTyped(java.awt.event.KeyEvent evt) {
                    parametro_busquedaKeyTyped(evt);
                }
            });

            Tabla.setModel(model);
            Tabla.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
                public void propertyChange(java.beans.PropertyChangeEvent evt) {
                    TablaPropertyChange(evt);
                }
            });
            jScrollPane1.setViewportView(Tabla);

            selectAll.setFont(new java.awt.Font("Arial", 1, 14));
            selectAll.setText("Seleccionar Todos");
            selectAll.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    selectAllActionPerformed(evt);
                }
            });

            jLabel1.setFont(new java.awt.Font("Arial", 0, 36));
            jLabel1.setText("Búsqueda Causas");

            jButton_UsarSeleccionado.setFont(new java.awt.Font("Arial", 0, 14));
            jButton_UsarSeleccionado.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Vista/Imagenes_Files/buscar_cliente/24.png"))); // NOI18N
            jButton_UsarSeleccionado.setText("Usar Seleccionado");
            jButton_UsarSeleccionado.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton_UsarSeleccionadoActionPerformed(evt);
                }
            });

            jButton_Cancelar.setFont(new java.awt.Font("Arial", 0, 14));
            jButton_Cancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Vista/Imagenes_Files/cruz/24.png"))); // NOI18N
            jButton_Cancelar.setText("Cancelar");
            jButton_Cancelar.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton_CancelarActionPerformed(evt);
                }
            });

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(Bar, javax.swing.GroupLayout.DEFAULT_SIZE, 1000, Short.MAX_VALUE)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 641, Short.MAX_VALUE)
                            .addComponent(label_Buscar481, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 980, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(parametro_busqueda, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(26, 26, 26)
                            .addComponent(selectAll)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jButton_UsarSeleccionado)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jButton_Cancelar)
                            .addGap(0, 216, Short.MAX_VALUE)))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(19, 19, 19)
                            .addComponent(jLabel1))
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(label_Buscar481, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(parametro_busqueda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(selectAll)
                        .addComponent(jButton_Cancelar)
                        .addComponent(jButton_UsarSeleccionado))
                    .addGap(18, 18, 18)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 389, Short.MAX_VALUE)
                    .addGap(18, 18, 18)
                    .addComponent(Bar, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
            );
        }// </editor-fold>//GEN-END:initComponents

private void parametro_busquedaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_parametro_busquedaKeyReleased
// TODO add your handling code here:
       if(!parametro_busqueda.getText().isEmpty()){
          cleanModel(model);
          Busqueda.busqueda_causa(parametro_busqueda.getText().toLowerCase(),model);
    }
   
   
   
}//GEN-LAST:event_parametro_busquedaKeyReleased

public static ArrayList<String> getSelectedRowsID(DefaultTableModel model){

    ArrayList<String> lista = new ArrayList();
        for (int i = 0; i < model.getRowCount(); i++) {
            boolean sel = (Boolean)model.getValueAt(i, 0);
            if(sel){                
                lista.add((model.getValueAt(i, 1)).toString());
            }     
        }
        return lista;
    }
/*
 * HOLA
 */
private void parametro_busquedaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_parametro_busquedaKeyPressed
// TODO add your handling code here:
   
}//GEN-LAST:event_parametro_busquedaKeyPressed

private void parametro_busquedaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_parametro_busquedaKeyTyped
// TODO add your handling code here:
     
}//GEN-LAST:event_parametro_busquedaKeyTyped

private void parametro_busquedaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_parametro_busquedaActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_parametro_busquedaActionPerformed

private void TablaPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_TablaPropertyChange
// TODO add your handling code here
   
}//GEN-LAST:event_TablaPropertyChange

    private void jButton_UsarSeleccionadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_UsarSeleccionadoActionPerformed
        // TODO add your handling code here:
        ArrayList<ArrayList> lista_causas_productos = new ArrayList<ArrayList>();
        ArrayList<String> selectedRowsID = getSelectedRowsID(this.tmodel_causa);
        ArrayList IDProductos = new ArrayList();
        ArrayList temp2 = new ArrayList();
        for (Iterator<String> it = selectedRowsID.iterator(); it.hasNext();) {
            String string = it.next();
            ArrayList Temp = Recepcion_Judicial.get_Lista_Productos(string);
            IDProductos.add(Temp.get(1));
        }
        for (int i = 0; i < IDProductos.size(); i++) {
            temp2 = Producto.getDatos(IDProductos.get(i).toString());
            lista_causas_productos.add(temp2);
        }
        
        //P_Remate.tabla_Producto1.AgregaProductos(lista_causas_productos,false);
        //P_Remate.tabla_Producto1.AgregaProductos();
        
        V_Principal.removePanel(this);
        V_Principal.desbloquearPanel();
        this.Hilo(false);
        /*
        if(!selectedRowsID.isEmpty()){
            ArrayList Temp = Recepcion_Judicial.get_Lista_Productos(RUT);
            P_Revoluntaria.TextField_RUT.setText(RUT);
            String Nombre = Temp.get(3).toString() +" "+Temp.get(1).toString() + " " + Temp.get(2).toString();
            P_Revoluntaria.jTextField_Nombre.setText(Nombre);
            V_Principal.removePanel(this);
            V_Principal.desbloquearPanel();
            this.Hilo(false);
        }
        
        else
            JOptionPane.showMessageDialog(V_Principal, "Seleccione algún criterio de búsqueda", "Error", JOptionPane.ERROR_MESSAGE);
          */  
    }//GEN-LAST:event_jButton_UsarSeleccionadoActionPerformed

    private void jButton_CancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_CancelarActionPerformed
        // TODO add your handling code here:
        V_Principal.removePanel(this);
        V_Principal.desbloquearPanel();
        this.Hilo(false);
    }//GEN-LAST:event_jButton_CancelarActionPerformed

private void selectAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectAllActionPerformed
// TODO add your handling code here:
    if(selectAll.isSelected()){
        for (int i = 0; i < model.getRowCount(); i++) {
        model.setValueAt(true, i, 0);
        
    }
    }else{
        for (int i = 0; i < model.getRowCount(); i++) {
        model.setValueAt(false, i, 0);
        
    }
    }
}//GEN-LAST:event_selectAllActionPerformed

public void cleanModel(DefaultTableModel model){
    int a =model.getRowCount()-1;

    for (int i = a; i >=0; i--) {
        model.removeRow(i);
        
    }

    
}

class taskInitContents extends Thread{
      
      public void run() {
       
           Bar.setVisible(true);
           Bar.setMaximum(1);
           Bar.setIndeterminate(true);
           Modelo.Busqueda.initCausas();
           Bar.setIndeterminate(true);
           Bar.setValue(1);
           Bar.setVisible(false);
        }
    }



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JProgressBar Bar;
    private javax.swing.JTable Tabla;
    private javax.swing.JButton jButton_Cancelar;
    private javax.swing.JButton jButton_UsarSeleccionado;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private Vista.Imagenes_Label.buscar.Label_Buscar48 label_Buscar481;
    private javax.swing.JTextField parametro_busqueda;
    private javax.swing.JCheckBox selectAll;
    // End of variables declaration//GEN-END:variables
}